/** init domain config */
// import './config'

import Vue from 'vue'
import App from './App.vue'
import Storage from 'vue-ls'
import router from './router'
import store from './store/'
import { VueAxios } from "@/utils/request"
import '@/utils/directives.js'
import echarts from "echarts";
import "echarts-wordcloud/dist/echarts-wordcloud";
import "echarts-wordcloud/dist/echarts-wordcloud.min";
Vue.prototype.$echarts = echarts;

require('@jeecg/antd-online-mini')
require('@jeecg/antd-online-mini/dist/OnlineForm.css')

import Antd, { version } from 'ant-design-vue'
//console.log('ant-design-vue version:', version)

import Viser from 'viser-vue'
import 'ant-design-vue/dist/antd.less';  // or 'ant-design-vue/dist/antd.less'
import BaiduMap from 'vue-baidu-map'

import '@/permission' // permission control
import '@/utils/filter' // base filter
import Print from 'vue-print-nb-jeecg'
/*import '@babel/polyfill'*/
import preview from 'vue-photo-preview'
import 'vue-photo-preview/dist/skin.css'
import SSO from '@/cas/sso.js'
import {
  ACCESS_TOKEN,
  DEFAULT_COLOR,
  DEFAULT_THEME,
  DEFAULT_LAYOUT_MODE,
  DEFAULT_COLOR_WEAK,
  SIDEBAR_TYPE,
  DEFAULT_FIXED_HEADER,
  DEFAULT_FIXED_HEADER_HIDDEN,
  DEFAULT_FIXED_SIDEMENU,
  DEFAULT_CONTENT_WIDTH_TYPE,
  DEFAULT_MULTI_PAGE
} from "@/store/mutation-types"
import config from '@/defaultSettings'

import JDictSelectTag from './components/dict/index.js'
import hasPermission from '@/utils/hasPermission'
import vueBus from '@/utils/vueBus';
import JeecgComponents from '@/components/jeecg/index'
import '@/assets/less/JAreaLinkage.less'
import VueAreaLinkage from 'vue-area-linkage'
import '@/components/jeecg/JVxeTable/install'
import '@/components/JVxeCells/install'
//表单验证
import { rules } from '@/utils/rules'

/* import Prints from '@/utils/print.js'
Vue.use(Prints) */

import i18n from '@/lang/index'

Vue.prototype.rules = rules
Vue.config.productionTip = false
Vue.use(Storage, config.storageOptions)
Vue.use(Antd)
Vue.use(VueAxios, router)
Vue.use(Viser)
Vue.use(hasPermission)
Vue.use(JDictSelectTag)
Vue.use(Print)
Vue.use(preview)
Vue.use(vueBus);
Vue.use(JeecgComponents);
Vue.use(VueAreaLinkage);
Vue.use(BaiduMap, {
  ak: 'oW2UEhdth2tRbEE4FUpF9E5YVDCIPYih',
  type: 'WebGL'
})


Vue.directive('enterToNext',{
  inserted:function(el){
    let inputs = el.querySelectorAll('input');
    let buttons = el.querySelectorAll('button');
    let bLen=buttons.length;
    //绑定回写事件
    for( var i = 0 ;i < inputs.length ; i++ ){
      inputs[i].setAttribute("keyFocusIndex",i);
      inputs[i].addEventListener('keyup', (ev) => {
        if (ev.keyCode === 13) {
          let targetTo = ev.srcElement.getAttribute('keyFocusTo');
          if(targetTo){
            this.$refs[targetTo].$el.focus();
          }else{
            var attrIndex = ev.srcElement.getAttribute('keyFocusIndex');
            var ctlI = parseInt(attrIndex);
            if(ctlI<inputs.length-1){
              if(inputs[ctlI].selectionStart!=0){
                inputs[ctlI].parentNode.parentNode.blur();
                inputs[ctlI+1].focus();
              }else{
                inputs[ctlI+1].focus();
              }
            }else{
              if(buttons.length>0){
                buttons[bLen-1].focus();
                buttons[bLen-1].click();
              }
            }
          }
        }
      });
    }
  },
});


SSO.init(() => {
  main()
})
function main() {
  new Vue({
    router,
    store,
    i18n,
    mounted () {
      store.commit('SET_SIDEBAR_TYPE', Vue.ls.get(SIDEBAR_TYPE, true))
      store.commit('TOGGLE_THEME', Vue.ls.get(DEFAULT_THEME, config.navTheme))
      store.commit('TOGGLE_LAYOUT_MODE', Vue.ls.get(DEFAULT_LAYOUT_MODE, config.layout))
      store.commit('TOGGLE_FIXED_HEADER', Vue.ls.get(DEFAULT_FIXED_HEADER, config.fixedHeader))
      store.commit('TOGGLE_FIXED_SIDERBAR', Vue.ls.get(DEFAULT_FIXED_SIDEMENU, config.fixSiderbar))
      store.commit('TOGGLE_CONTENT_WIDTH', Vue.ls.get(DEFAULT_CONTENT_WIDTH_TYPE, config.contentWidth))
      store.commit('TOGGLE_FIXED_HEADER_HIDDEN', Vue.ls.get(DEFAULT_FIXED_HEADER_HIDDEN, config.autoHideHeader))
      store.commit('TOGGLE_WEAK', Vue.ls.get(DEFAULT_COLOR_WEAK, config.colorWeak))
      store.commit('TOGGLE_COLOR', Vue.ls.get(DEFAULT_COLOR, config.primaryColor))
      store.commit('SET_TOKEN', Vue.ls.get(ACCESS_TOKEN))
      store.commit('SET_MULTI_PAGE',Vue.ls.get(DEFAULT_MULTI_PAGE,config.multipage))
    },
    render: h => h(App)
  }).$mount('#app')
}

(function () {
  const originalToFixed = Number.prototype.toFixed;
  const originalParseFloat = parseFloat;

  Number.prototype.toFixed = function (digits = 0) {
    // 参数验证，与原生的行为保持一致
    if (digits < 0 || digits > 20) {
      throw new RangeError('toFixed() digits argument must be between 0 and 20');
    }

    // 调用原生的toFixed方法获取字符串
    const str = originalToFixed.call(this, digits);
    var qianFenFlag= localStorage.getItem("thousandsSeparator");
    if (qianFenFlag==0){
      return str;
    }
    //return str;
    // 分割整数部分和小数部分
    const parts = str.split('.');
    let integerStr = parts[0];
    const decimalStr = parts.length > 1 ? `.${parts[1]}` : '';

    // 处理负号
    let sign = '';
    if (integerStr.startsWith('-')) {
      sign = '-';
      integerStr = integerStr.substring(1);
    }

    // 添加千分位分隔符
    integerStr = integerStr.replace(/\B(?=(\d{3})+(?!\d))/g, ',');
    // 合并结果
    return sign + integerStr + decimalStr;
  };
  // 重写全局 parseFloat
  window.parseFloat = function (string) {
    // 预处理字符串：移除千分位逗号
    const sanitizedString = String(string).replace(/,/g, '');
    // 调用原生 parseFloat
    return originalParseFloat(sanitizedString);
  };

  // 可选：提供恢复原生方法的能力
  window.parseFloat.restore = function () {
    window.parseFloat = originalParseFloat;
  };

})();